﻿using System;
using RootMotion.FinalIK;
using UnityEngine;

namespace RootMotion.Demos
{
	// Token: 0x02000095 RID: 149
	public class AnimationWarping : OffsetModifier
	{
		// Token: 0x06000538 RID: 1336 RVA: 0x00005493 File Offset: 0x00003693
		public override void Start()
		{
			base.Start();
			this.lastMode = this.effectorMode;
		}

		// Token: 0x06000539 RID: 1337 RVA: 0x00033D00 File Offset: 0x00031F00
		public float GetWarpWeight(int warpIndex)
		{
			if (warpIndex < 0)
			{
				Debug.LogError("Warp index out of range.");
				return 0f;
			}
			if (warpIndex >= this.warps.Length)
			{
				Debug.LogError("Warp index out of range.");
				return 0f;
			}
			if (this.animator == null)
			{
				Debug.LogError("Animator unassigned in AnimationWarping");
				return 0f;
			}
			AnimatorStateInfo currentAnimatorStateInfo = this.animator.GetCurrentAnimatorStateInfo(this.warps[warpIndex].animationLayer);
			if (!currentAnimatorStateInfo.IsName(this.warps[warpIndex].animationState))
			{
				return 0f;
			}
			return this.warps[warpIndex].weightCurve.Evaluate(currentAnimatorStateInfo.normalizedTime - (float)((int)currentAnimatorStateInfo.normalizedTime));
		}

		// Token: 0x0600053A RID: 1338 RVA: 0x00033DCC File Offset: 0x00031FCC
		public override void OnModifyOffset()
		{
			for (int i = 0; i < this.warps.Length; i++)
			{
				float warpWeight = this.GetWarpWeight(i);
				Vector3 vector = this.warps[i].warpTo.position - this.warps[i].warpFrom.position;
				AnimationWarping.EffectorMode effectorMode = this.effectorMode;
				if (effectorMode != AnimationWarping.EffectorMode.PositionOffset)
				{
					if (effectorMode == AnimationWarping.EffectorMode.Position)
					{
						this.ik.solver.GetEffector(this.warps[i].effector).position = this.ik.solver.GetEffector(this.warps[i].effector).bone.position + vector;
						this.ik.solver.GetEffector(this.warps[i].effector).positionWeight = this.weight * warpWeight;
					}
				}
				else
				{
					this.ik.solver.GetEffector(this.warps[i].effector).positionOffset += vector * warpWeight * this.weight;
				}
			}
			if (this.lastMode == AnimationWarping.EffectorMode.Position && this.effectorMode == AnimationWarping.EffectorMode.PositionOffset)
			{
				foreach (AnimationWarping.Warp warp in this.warps)
				{
					this.ik.solver.GetEffector(warp.effector).positionWeight = 0f;
				}
			}
			this.lastMode = this.effectorMode;
		}

		// Token: 0x0600053B RID: 1339 RVA: 0x00033F90 File Offset: 0x00032190
		public void OnDisable()
		{
			if (this.effectorMode != AnimationWarping.EffectorMode.Position)
			{
				return;
			}
			foreach (AnimationWarping.Warp warp in this.warps)
			{
				this.ik.solver.GetEffector(warp.effector).positionWeight = 0f;
			}
		}

		// Token: 0x040003FD RID: 1021
		[Tooltip("Reference to the Animator component to use")]
		public Animator animator;

		// Token: 0x040003FE RID: 1022
		[Tooltip("Using effector.positionOffset or effector.position with effector.positionWeight? The former will enable you to use effector.position for other things, the latter will weigh in the effectors, hence using Reach and Pull in the process.")]
		public AnimationWarping.EffectorMode effectorMode;

		// Token: 0x040003FF RID: 1023
		[Space(10f)]
		[Tooltip("The array of warps, can have multiple simultaneous warps.")]
		public AnimationWarping.Warp[] warps;

		// Token: 0x04000400 RID: 1024
		public AnimationWarping.EffectorMode lastMode;

		// Token: 0x02000096 RID: 150
		[Serializable]
		public struct Warp
		{
			// Token: 0x04000401 RID: 1025
			[Tooltip("Layer of the 'Animation State' in the Animator.")]
			public int animationLayer;

			// Token: 0x04000402 RID: 1026
			[Tooltip("Name of the state in the Animator to warp.")]
			public string animationState;

			// Token: 0x04000403 RID: 1027
			[Tooltip("Warping weight by normalized time of the animation state.")]
			public AnimationCurve weightCurve;

			// Token: 0x04000404 RID: 1028
			[Tooltip("Animated point to warp from. This should be in character space so keep this Transform parented to the root of the character.")]
			public Transform warpFrom;

			// Token: 0x04000405 RID: 1029
			[Tooltip("World space point to warp to.")]
			public Transform warpTo;

			// Token: 0x04000406 RID: 1030
			[Tooltip("Which FBBIK effector to use?")]
			public FullBodyBipedEffector effector;
		}

		// Token: 0x02000097 RID: 151
		[Serializable]
		public enum EffectorMode
		{
			// Token: 0x04000408 RID: 1032
			PositionOffset,
			// Token: 0x04000409 RID: 1033
			Position
		}
	}
}
